//
//  TLHistoryController.m
//  Books
//
//  Created by tao故乡的云 on 2017/3/17.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "TLHistoryController.h"
#import "TLChengYu.h"
#import "TLReadPage.h"

@interface TLHistoryController ()
@property(strong, nonatomic) NSMutableArray *historyItems;
@end

@implementation TLHistoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)historyItems {
    if (_historyItems == nil) {
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *path = [cachePath stringByAppendingPathComponent:@"historyItems.plist"];
        _historyItems = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        if (_historyItems == nil) {
            _historyItems = [NSMutableArray array];
        }
    }
    
    return _historyItems;
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.historyItems.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.textColor = [UIColor orangeColor];
    }
    TLChengYu *cy = self.historyItems[indexPath.row];
    cell.textLabel.text = cy.name;
    cell.detailTextLabel.text = cy.pinYin;
    
    return cell;
}

#pragma mark - Table view data delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TLReadPage *read = [[TLReadPage alloc] init];
    read.items = self.historyItems;
    read.currentItem = indexPath.row;
    read.type = TLReadPageTypeHistory;
    [self.navigationController pushViewController:read animated:YES];
}


@end
